"""
@Project ：Python_Learning 
@File    ：5.with语句.py
@IDE     ：PyCharm 
@Author  ：zhupp
@Date    ：2024/10/4 16:53 
"""

# class Sample:
#
#     def __enter__(self):
#         try:
#             self.file_obj = open('file.txt')
#         except FileNotFoundError:
#             self.file_obj = None
#         print('我被执行了：__enter__')
#         return self
#
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         print('我被执行了：__exit__')
#         if self.file_obj is None:
#             print('当前文件不存在')
#         else:
#             self.file_obj.close()
#
#     def run(self):
#         print("111")
#
#
# with Sample() as sample:
#     sample.run()

def count_up_to(n):
    count = 1
    while count <= n:
        yield count
        count += 1


# 创建生成器对象
counter = count_up_to(5)

# 逐个获取生成器的值
for number in counter:
    print(number)
# 输出: 1 2 3 4 5